package factorytest.iflytek.com.einkiflytekfactorytest.engineer_test;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.io.File;

import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.common.FTUtils;
import factorytest.iflytek.com.einkiflytekfactorytest.myrecorder.RecorderThread;
import factorytest.iflytek.com.einkiflytekfactorytest.playerutils.MediaPlayerTool;

public class FragmentES7210_32And16Bit extends BaseFragment implements View.OnClickListener{
    private static final String RECORDER_32BIT_FILE_PATH = FTUtils.getFTStorageDirectory() + File.separator + "mic_es7210_32bit.pcm";
    private static final String RECORDER_16BIT_FILE_PATH = FTUtils.getFTStorageDirectory() + File.separator + "mic_es7210_16bit.pcm";

    private final int SampleRateInHz = 16000;

    private RadioGroup radioGroupBitSelection;
    private boolean is32Bit = true;

    private Button btnPlay32Bit;
    private Button btnPlay16Bit;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_engineer_es7210_32bit_16bit;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {

        initBtn(R.id.btnRecord);
        initBtn(R.id.btnPlay32Bit);
        initBtn(R.id.btnPlay16Bit);
        btnPlay32Bit = (Button) findViewById(R.id.btnPlay32Bit);
        btnPlay16Bit = (Button) findViewById(R.id.btnPlay16Bit);

        radioGroupBitSelection = (RadioGroup) findViewById(R.id.radioGroupBitSelection);
        radioGroupBitSelection.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int id = group.getCheckedRadioButtonId();
                switch (group.getCheckedRadioButtonId()) {
                    case R.id.radioBtn32Bit:
                        is32Bit = true;
                        Toast.makeText(mContext, "32bit", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.radioBtn16Bit:
                        is32Bit = false;
                        Toast.makeText(mContext, "16bit", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        });

        updateBtnRecord();

        AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        am.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);

    }

    @Override
    protected void onVisible() {
        super.onVisible();

    }

    @Override
    protected void onInvisible() {
        super.onInvisible();
        doStopRecord();

    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (id == R.id.btnRecord) {
            clickBtnRecorder();
        } else if (id == R.id.btnPlay16Bit) {
            startPlay(false);
        } else if (id == R.id.btnPlay32Bit) {
            startPlay(true);
        }

    }

    private void doUpdateStatus(int statusResId) {
        TextView txtStatus = (TextView) findViewById(R.id.txtStatus);
        if (txtStatus == null) {
            return;
        }
        txtStatus.setText(statusResId);
    }

    private void doStopRecord() {
        stopTestRecord();

        doUpdateStatus(R.string.mic_status_recording_stop);
    }

    private void doStartRecord() {
        startTestRecord();

        doUpdateStatus(R.string.mic_status_recording);
    }

    private void clickBtnRecorder() {
        if (recorderThread != null) {
            doStopRecord();
        } else {
            doStartRecord();
        }

        updateBtnRecord();
    }


    private void initBtn(int resId) {
        View view = findViewById(resId);
        if (view != null) {
            view.setOnClickListener(this);
        }
    }

    private void updateBtnRecord() {
        Button btnRecord = (Button) findViewById(R.id.btnRecord);
        if (btnRecord == null) {
            return;
        }
        if (recorderThread != null) {
            btnRecord.setText(R.string.record_stop);

            radioGroupBitSelection.setEnabled(false);

        } else {
            btnRecord.setText(R.string.record_start);
            radioGroupBitSelection.setEnabled(true);
        }
    }

    //////////////////////
    //    Recorder
    /////////////////////
    private RecorderThread recorderThread;

    public void startTestRecord() {
        startRecordTread();
    }

    private void stopTestRecord() {
        stopRecordThread();

    }

    private void startRecordTread() {

        if (recorderThread != null) {
            stopRecordThread();
        }

        recorderThread = new RecorderThread("es7210-", null,
                is32Bit ? RECORDER_32BIT_FILE_PATH : RECORDER_16BIT_FILE_PATH);
        recorderThread.getRecorderOperator().setSampleRateInHz(SampleRateInHz);
        recorderThread.getRecorderOperator().setChannelConfig(AudioFormat.CHANNEL_IN_STEREO);
        recorderThread.getRecorderOperator().setAudioFormat(is32Bit ? AudioFormat.ENCODING_PCM_FLOAT : AudioFormat.ENCODING_PCM_16BIT);

        recorderThread.start();

    }

    private void stopRecordThread() {

        if (recorderThread != null) {
            recorderThread.joinThread();
            recorderThread = null;
        }
    }


    //////////////////////
    //    play
    /////////////////////
    private MediaPlayerTool mediaPlayerTool;

    private void startPlay(boolean is32Bit) {
        if (mediaPlayerTool == null) {
            mediaPlayerTool = MediaPlayerTool.getInstance(getContext(), getHandler());
        }

        mediaPlayerTool.playPcm(is32Bit ? RECORDER_32BIT_FILE_PATH : RECORDER_16BIT_FILE_PATH);

    }

    private void stopPlay() {
        mediaPlayerTool.stop();
    }


}
